<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script type="text/javascript" charset="utf-8">
var admin_kf_datagrid;
var admin_kf_editRow = undefined;
var admin_kf_addRow = undefined;
var admin_kf_searchForm = undefined;
$(function(){
		
	admin_kf_searchForm = $('#admin_kf_searchForm').form();
	  $("#admin_kf_searchButton").bind('click', function(){   
		  
		  admin_kf_search(); 
	    }); 
	  $("#admin_kf_CleanSearchButton").bind('click', function(){   
		  
		  admin_kf_cleanSearch();
    }); 
		
	  admin_kf_datagrid=$('#admin_kf_datagrid').datagrid({
		url : '${pageContext.request.contextPath}/kfAction!dataGrid.action',
		title:'库房信息查询',
		iconCls:'icon-search',
		toolbar : '#admin_kf_toolbar',
		fit : true,
		border : false,
		//animate : true,
		//singleSelect : false,	//false是打开全选功能
		striped:true,
		idField : 'id',
		rownumbers: true,		//显示行号
		fitColumns : true,		//显示横向滚动条
		pagination : true,
		pageSize : 20,
		pageList : [ 10,20, 30, 40, 50 ],
		sortName : 'cjsj',
		sortOrder : 'asc',
		frozenColumns:[[
				{
					title:'编号',
					field:'id',
					width:100,
					checkbox : true,
				},
				{
					title:'库房编号',
					field:'numberID',
					width:100,
					sortable:'true',
					editor:{
						type:'validatebox',
						options:{
							required:true, 
							missingMessage:'库房编号必填'
						}
					}
				}
		]],	
		columns : [[{
					title:'库房名称',
					field:'name',
					width:100,
					sortable:'true',
					editor:{
						type:'text',
					}
				},{
					title : '备注',
					field : 'bz',
					width : 130,
					align : 'center',
					sortable:true,
					editor:{
						type:'text'
					}
			
				},{
					title : '状态',
					field : 'flag',
					width : 80,
					align : 'left',
					sortable:true,
					formatter:function(value){
						if(value == "T"){
							return "开启";
						}else{
							return "关闭";
						}
					},
					editor:{
						type:'combobox',
						options:{
							required:true,
							valueField:'flag',
							textField:'txtValue',
							data: [{
									txtValue: '开启',
									flag: 'T'
								},{
									txtValue: '关闭',
									flag: 'F'
								}],
							editable:false,
							missingMessage:'状态必填'
							
						}
					}
			
				},{
					title:'创建时间',
					field:'cjsj',
					width:100,
					sortable:true,
					
				}]],
		
			onAfterEdit : function(rowIndex, rowData, changes) {
				var inserted = admin_kf_datagrid.datagrid('getChanges', 'inserted');
				var updated = admin_kf_datagrid.datagrid('getChanges', 'updated');
				if (inserted.length < 1 && updated.length < 1) {
					admin_kf_editRow = undefined;
					admin_kf_addRow = undefined;
					admin_kf_datagrid.datagrid('unselectAll');
					return;
				}
				var url = '';
				if (inserted.length > 0) {
					url = '${pageContext.request.contextPath}/kfAction!add.action';
				}
				if (updated.length > 0) {
					url = '${pageContext.request.contextPath}/kfAction!edit.action';
				}
				$.ajax({
					url : url,
					data : rowData,
					dataType : 'json',
					type:'POST',
					success : function(r) {
						if (r.success) {
							admin_kf_datagrid.datagrid('acceptChanges');
							$.messager.show({
								msg : r.msg,
								title : '成功'
							});
							admin_kf_editRow = undefined;
							admin_kf_addRow = undefined;
							admin_kf_datagrid.datagrid('load');
						} else {
							admin_kf_datagrid.datagrid('rejectChanges');
							$.messager.alert('错误', r.msg, 'error');
						}
						admin_kf_datagrid.datagrid('unselectAll');
						admin_kf_editRow = undefined;
						admin_kf_addRow = undefined;
					}
				});
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#admin_kf_easyui-menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			},
			onDblClickRow : function(rowIndex, rowData) {
				if(admin_kf_addRow!=undefined){
					$.messager.show({
						msg : "请先保存或撤销编辑！",
						title : '提示',
					});
					return;
				}else{
					
					if (admin_kf_editRow != undefined) {
						admin_kf_datagrid.datagrid('endEdit', admin_kf_editRow);
					}

					if (admin_kf_editRow == undefined) {
						admin_kf_datagrid.datagrid('beginEdit', rowIndex);
						admin_kf_editRow = rowIndex;
						admin_kf_datagrid.datagrid('unselectAll');
					}
				}
				
			}
		
		
			});
			
		});
		function admin_kf_appendColumns(){
			
			if (admin_kf_editRow != undefined||admin_kf_addRow!=undefined) {
				$.messager.show({
					msg : "请先保存或撤销编辑！",
					title : '提示',
				});
				return;
			}
		
			else{
				admin_kf_datagrid.datagrid('unselectAll');
		
		
				var row = {};
				admin_kf_datagrid.datagrid('insertRow', {
					index : 0,
					row : row
				});
				admin_kf_addRow = 0;
				admin_kf_datagrid.datagrid('selectRow', admin_kf_addRow);
				admin_kf_datagrid.datagrid('beginEdit', admin_kf_addRow);
			}
		}

		function admin_kf_edit() {
			var rows = admin_kf_datagrid.datagrid('getSelections');
			if (rows.length == 1) {
				if(admin_kf_addRow!=undefined){
					$.messager.show({
						msg : "请先保存或撤销编辑！",
						title : '提示',
					});
					return;
				}else{
					if (admin_kf_editRow != undefined) {
						admin_kf_datagrid.datagrid('endEdit', admin_kf_editRow);
					}
					if (admin_kf_editRow == undefined) {
						admin_kf_editRow = admin_kf_datagrid.datagrid('getRowIndex', rows[0]);
						admin_kf_datagrid.datagrid('beginEdit', admin_kf_editRow);
						admin_kf_datagrid.datagrid('unselectAll');
					}
				}
				
			} else {
				$.messager.show({
					msg : '请选择一项进行修改！',
					title : '错误'
				});
			}
		}
		function admin_kf_search() {
			if(admin_kf_editRow != undefined||admin_kf_addRow!=undefined){
				$.messager.show({
					msg : '请先保存或撤销编辑！',
					title : '提示'
				});
			}else{
				admin_kf_datagrid.datagrid('load', serializeObject(admin_kf_searchForm));
			}
			
		}
		function admin_kf_cleanSearch() {
			admin_kf_editRow = undefined;
			admin_kf_addRow=undefined;
			admin_kf_datagrid.datagrid('load', {});
			admin_kf_searchForm.find('input').val('');
		}
		function admin_kf_save(){
			if (admin_kf_editRow != undefined) {
				admin_kf_datagrid.datagrid('endEdit', admin_kf_editRow);
				admin_kf_editRow = undefined;
			}
			if (admin_kf_addRow != undefined) {
					var aaa=admin_kf_datagrid.datagrid('endEdit', admin_kf_addRow);
					admin_kf_datagrid.datagrid('getChanges', 'inserted');
				
			}
		}
		function admin_kf_unedit(){
			admin_kf_datagrid.datagrid('unselectAll');
			admin_kf_datagrid.datagrid('rejectChanges');
			admin_kf_editRow = undefined;
			admin_kf_addRow = undefined;
		}
</script>
<div id="admin_kf_layout" class="easyui-layout" data-options="fit:true,border:false">

	<div region="center" border="false" style="overflow: hidden;">
		<div id="admin_kf_toolbar" class="datagrid-toolbar" style="height: auto;display: none;">
			<fieldset>
				<legend>筛选</legend>
				<form id="admin_kf_searchForm">
					<table class="admin_kf_searchForm datagrid-toolbar" style="width: 100%;height: 100%;">
					<tr>
					<td>
					<label>库房编号:&nbsp</label><input type="text" name="numberID" style="width:150px;" />
					<a href='javascript:void(0);' id="admin_kf_searchButton" class='easyui-linkbutton' data-options="iconCls:'icon-search'">查询</a>
					<a href='javascript:void(0);' id="admin_kf_CleanSearchButton" class='easyui-linkbutton' data-options="iconCls:'icon-cancel'">清空</a>
					</td>
					</tr>
					</table>
				</form>
			</fieldset>
			<div>
				<a class="easyui-linkbutton" iconCls="icon-add" onclick="admin_kf_appendColumns();" plain="true" href="javascript:void(0);">增加</a> 
				<a class="easyui-linkbutton" iconCls="icon-edit" onclick="admin_kf_edit();" plain="true" href="javascript:void(0);">编辑</a> 
				<a class="easyui-linkbutton" iconCls="icon-save" onclick="admin_kf_save();" plain="true" href="javascript:void(0);">保存</a> 
				<a class="easyui-linkbutton" iconCls="icon-undo" onclick="admin_kf_unedit();" plain="true" href="javascript:void(0);">取消编辑</a> 
				<a class="easyui-linkbutton" iconCls="icon-undo" onclick="admin_kf_datagrid.datagrid('unselectAll');" plain="true" href="javascript:void(0);">取消选中</a>
			</div>
		</div>
		<table id="admin_kf_datagrid"></table>
	</div>
</div>